var integerReg = /^[0-9\ ]+$/;
var decimalReg = /^\d{0,12}\.{0,1}(\d{1,2})?$/;
$(function () {
    $('input.id').attr('maxlength', 10);
    $('input.serial_number').attr('maxlength', 20);
});

function checkInteger(t) {
    var val = $(t).val().replace(/\s/g, '');
    if (val === '') {
        $(t).attr('record', val);
    } else if (integerReg.test($(t).val())) {
        $(t).val(val).attr('record', val);
    } else {
        $(t).val($(t).attr('record'));
    }
}

function checkDecimal(t) {
    if ($(t).val() === '') {
        $(t).val('');
    } else if ($(t).val() === '.') {
        $(t).val('');
    } else if (decimalReg.test($(t).val())) {
        $(t).attr('record', $(t).val());
    } else {
        $(t).val($(t).attr('record'));
    }
}

function adminAjax(url, href, data) {
    data['_token'] = $.admin.token;
    comAjax('/admin/' + url, href, data);
}

function comAjax(url, href, data, headers) {
    $('body').append('<div class="ycxq-loading-mask">加载中...</div>');
    $.ajax({
        url: url,
        type: 'post',
        data: JSON.stringify(data),
        headers: headers,
        contentType: 'application/json; charset=utf-8',
        processData: false,
        cache: false,
        dataType: 'json',
        success: function (data) {
            if (data.code === 200) window.location.href = '/admin/' + href;
            else alert(data.message || data.msg);
        },
        complete: function () {
            $('.ycxq-loading-mask').remove();
        }
    })
}

function apiAjax(url, href, newAuth, data) {
    comAjax('/v1/api/' + url, href, data, {newAuth: newAuth});
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}
